﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using timetableasy.Models;

namespace timetableasy.Controllers
{
    public class CourseProgramsController : Controller
    {
        CourseProgramRepository courseProgramRepository = new CourseProgramRepository();
        //
        // GET: /CoursePrograms/

        public ActionResult Index()
        {
            var coursePrograms = courseProgramRepository.FindAllCoursePrograms().ToList();
            return View("Index", coursePrograms);
        }

        //
        // GET: /CoursePrograms/Details/5

        public ActionResult Details(int id)
        {
            CourseProgram courseProgram = courseProgramRepository.GetCourseProgramById(id);
            if (courseProgram == null)
                return View("NotFound");
            else
                return View("Details", courseProgram);
        }

        //
        // GET: /CoursePrograms/Create

        public ActionResult Create()
        {
            CourseProgram courseProgram = new CourseProgram()
            {
                CourseProgramName = string.Empty,
                TimeVolume = 1,
                Period = new Period()
            };

            return View(courseProgram);
        }

        //
        // POST: /CoursePrograms/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(CourseProgram courseProgram)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    courseProgramRepository.Add(courseProgram);
                    courseProgramRepository.Save();

                    return RedirectToAction("Details", new { id = courseProgram.CourseProgramId });
                }
                catch
                {
                    foreach (var issue in courseProgram.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }
                }
            }

            return View(courseProgram);
        }

        //
        // GET: /CoursePrograms/Edit/5

        public ActionResult Edit(int id)
        {
            CourseProgram courseProgram = courseProgramRepository.GetCourseProgramById(id);
            if (courseProgram != null)
            {
                return View(courseProgram);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // POST: /CoursePrograms/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            CourseProgram courseProgram = courseProgramRepository.GetCourseProgramById(id);
            if (courseProgram == null)
                return View("NotFound");
            try
            {
                UpdateModel(courseProgram);
                courseProgramRepository.Save();
                return RedirectToAction("Details", new { id = courseProgram.CourseProgramId });
            }
            catch
            {
                foreach (var issue in courseProgram.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(courseProgram);
            }
        }

        //
        // GET: /CoursePrograms/Delete/5

        public ActionResult Delete(int id)
        {
            CourseProgram courseProgram = courseProgramRepository.GetCourseProgramById(id);

            if (courseProgram == null)
                return View("NotFound");

            //if (!CourseProgram.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            return View(courseProgram);
        }

        // 
        // HTTP POST: /CoursePrograms/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            CourseProgram courseProgram = courseProgramRepository.GetCourseProgramById(id);

            if (courseProgram == null)
                return View("NotFound");

            //if (!CourseProgram.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            courseProgramRepository.Delete(courseProgram);
            courseProgramRepository.Save();

            return View("Deleted");
        }
    }
}
